/*
 * Copyright (C) 2015  Vialatte Romain <romain.vialatte@ecole.ensicaen.fr>
 * Copyright (C) 2016  Nicola Spanti (RyDroid) <dev@nicola-spanti.info>
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 * 
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */


#include <check.h>
#include "PlanetWars2dRT-core/specific/controller/game_end_result.h"
#include "PlanetWars2dRT-core/specific/model/game.h"
#include "PlanetWars2dRT-core/specific/model/universe_planets.h"
#include "PlanetWars2dRT-core/specific/model/universe_ships.h"
#include "PlanetWars2dRT-core/specific/model/planet_default.h"
#include "PlanetWars2dRT-core/utils/color_rgb_const.h"


struct game_t   a_game;
struct player_t a_human;
struct planet_t a_planet;

void
tests_setup_init(void)
{
  a_game = game_create_default();
  universe_init_first_time_only(&a_game.universe);
  a_game.universe.size.x = 500;
  a_game.universe.size.y = 500;
  
  planet_set_default(&a_planet);
  a_planet.player_index = 0;
  a_planet.position.x =  50;
  a_planet.position.y =  50;
  
  universe_add_planet_unsafe(&a_game.universe, &a_planet);
  
  player_set_name(&a_human, "Anthony");
  a_human.color = COLOR_RGB_BLUE;
  game_add_player(&a_game, &a_human);
  
  player_set_name(&a_human, "Nicola");
  a_human.color = COLOR_RGB_RED;
  game_add_player(&a_game, &a_human);
}

void
tests_setup_free(void)
{
  game_destructor(&a_game);
}


START_TEST(test_incorrect_end_game)
{
  a_game.get_winner_index = NULL;
  ck_assert(!game_is_finished(&a_game));
}
END_TEST


START_TEST(test_correct_destruction_player_end_game)
{
  game_destructor(&a_game);
  ck_assert_ptr_eq(a_human.data, NULL);
}
END_TEST


START_TEST(test_correct_number_of_players_at_end_game)
{
  game_destructor(&a_game);
  ck_assert(game_get_nb_players(&a_game) == 0);
}
END_TEST


START_TEST(test_correct_number_of_planets_at_end_game)
{
  game_destructor(&a_game);
  ck_assert(universe_get_nb_planets(&a_game.universe) == 0);
}
END_TEST


START_TEST(test_correct_number_of_ships_at_end_game)
{
  game_destructor(&a_game);
  ck_assert(universe_get_nb_ships(&a_game.universe) == 0);
}
END_TEST

	
Suite*
game_end_suite(void)
{
  Suite* s = suite_create("Correct end game");
  
  TCase* tc_correct_game = tcase_create("End game");
  tcase_add_checked_fixture(tc_correct_game,
			    tests_setup_init, tests_setup_free);
  
  tcase_add_test(tc_correct_game, test_incorrect_end_game);
  tcase_add_test(tc_correct_game, test_correct_destruction_player_end_game);
  tcase_add_test(tc_correct_game, test_correct_number_of_players_at_end_game);
  tcase_add_test(tc_correct_game, test_correct_number_of_planets_at_end_game);
  tcase_add_test(tc_correct_game, test_correct_number_of_ships_at_end_game);
  suite_add_tcase(s, tc_correct_game);
  
  return s;
}

int
main(void)
{
  size_t number_failed;
  Suite* s;
  SRunner* sr;
  
  s = game_end_suite();
  sr = srunner_create(s);
  
  srunner_run_all(sr, CK_NORMAL);
  number_failed = srunner_ntests_failed(sr);
  srunner_free(sr);
  
  return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
}
